package com.revzone.demo001.entity.OtherEntity;
import com.revzone.demo001.enums.ExciptionEnums.EnumExciption;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.HashMap;
import java.util.Map;

/**
 * 通用封装返回数据/页面响应entity
 */
@ApiModel("全局统一返回结果")
public class R extends HashMap<String, Object> {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "返回码", example = "200")
    private static Integer code;

    @ApiModelProperty(value = "返回消息", example = "成功")
    private String msg;

    @ApiModelProperty(value = "返回数据")
    private Map<String, Object> data;

    public R() {
        put("code", 200);
        put("msg", "success");
    }


    public static R error() {
        return error(500, "未知异常，请联系管理员");
    }

    public static R error(String msg) {
        return error(500, msg);
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        r.msg = msg;
        return r;
    }

    public static R error(EnumExciption enumExciption) {
        R r = new R();
        r.put("code", enumExciption.getCode());
        r.put("msg", enumExciption.getMsg());
        return r;
    }


    public static R ok(String msg) {
        R r = new R();
        r.put("msg", msg);
        return r;
    }

    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.putAll(map);
        return r;
    }

    public static R ok() {
        return new R();
    }

    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public static Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }
}
